<template>
	<div class="wrapper">
		<!--header部分-->
		<header>
			<div class="back" @click="toMy">
			<i class="fa fa-chevron-left"></i>
			</div>
			<div class="text">我的积分</div>
		</header>
		
		<!--资产部分-->
		<div class="assets">
			<div class="up">
				<div class="totalassets">
					<div class="text">拥有积分</div>
					<div class="number" >{{totalcredit}}</div>
				</div>
				<div class="detailed" @click="toCreditDetail">
					<i class="fa fa-list-ul"></i>
					<div class="text">全部明细</div>
				</div>
			</div>
		</div>
		<!--兑换积分部分-->
			<ul class="form-box">
				<li>
					<div class="title">
						兑换积分：
					</div>
					<div class="content">
						<input type="text" v-model="number" placeholder="100积分可兑换1元钱">
					</div>
				</li>
			</ul>
			<div class="button-cz">
				<button @click="dh">兑换</button>
			</div>
		</div>
	</div>
</template>

<script>
	export default{
		name:'integral',
		inject:['reload'],
		data(){
			return{
			user:{},
			number:'',
			creditArr:[],
			updateuser:{},
			}
		},
		created(){
			this.user = this.$getSessionStorage('user');
			this.$axios.post('CreditController/listByUserId',this.$qs.stringify({
				userId:this.user.userId,
			})).then(response=>{
				this.creditArr=response.data;
			}).catch(error => {
				console.error(error);
			});
			
			this.$axios.post('UserController/getUserByUserId',this.$qs.stringify({
				userId:this.user.userId,
			})).then(response=>{
				this.updateuser=response.data;
			}).catch(error => {
				console.error(error);
			});
		},
		computed: {//计算属性
		    totalcredit() {
		        let totalcredit = 0;
		        for (let item of this.creditArr) {
		            if(item.type==1){
						totalcredit+=item.credit;
					}else{
						totalcredit-=item.credit;
					}
		        }
		        return totalcredit;
		    }
		},
		methods:{
			
			dh(){
				if(this.totalcredit-this.number<0){
					alert('积分不足！');
				}
				else{
					this.$axios.post('CreditController/addByUserId',this.$qs.stringify({
						userId:this.user.userId,
						type:'2',
						credit:this.number,
						expired_time:'',
					})).then(response=>{
						
					}).catch(error=>{
						console.error(error);
					});
					
					this.$axios.post('UserController/addBalanceById',this.$qs.stringify({
						userId:this.user.userId,
						walletBalance:Number(this.updateuser.walletBalance)+Number(this.number/10),
					})).then(response=>{
						alert('兑换成功！')
					}).catch(error=>{
						console.error(error);
					});
					this.reload();
				}
			},
			toMy() {
				this.$router.push({
					path: '/my'
				});
			},
			toCreditDetail() {
				this.$router.push({
					path: '/creditDetail'
				});
			},
		}
	}
</script>

<style scoped>
	/****************** 总容器 ******************/
	.wrapper {
		width: 100%;
		height: 100%;
		background-color: azure;
	}
	/****************** header ******************/
	.wrapper header{
		width: 100%;
		height: 12vw;
		background-color: #FFF;
		display: flex;
		justify-content: flex-start;
		align-items: center;
		margin-bottom: 1vw;
	}
	.wrapper header .back{
		font-size: 4vw;
		margin-left: 2vw;
		margin-right: 2vw;
	}
	.wrapper header .text{
		font-size: 4vw;
	}
	/****************** 资产 ******************/
	.wrapper .assets{
		width: 100%;
		height: 14vw;
		background-color: whitesmoke;
		display: flex;
		
	}
	.wrapper .assets .up{
		width: 100%;
		height: 14vw;
		background-color: lightblue;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}
	.wrapper .assets .up .totalassets{
		width: 50%;
		height:14vw;
		display: flex;
		justify-content: flex-start;
		align-items: center;
	}
	.wrapper .assets .up .totalassets .text{
		margin-left: 2vw;
		margin-right: 3vw;
		font-size: 4vw;
	}
	.wrapper .assets .up .totalassets .number{
		font-size: 4vw;
	}
	.wrapper .assets .up .detailed{
		width: 50%;
		height: 14vw;
		display: flex;
		justify-content: flex-end;
		align-items: center;
	}
	.wrapper .assets .up .detailed i{
		font-size: 4vw;
	}
	.wrapper .assets .up .detailed .text{
		margin-left: 1vw;
		margin-right: 2vw;
		font-size: 4vw;
	}
	
	.wrapper .form-box {
		width: 100%;
		
	}
	
	.wrapper .form-box li {
		box-sizing: border-box;
		padding: 4vw 3vw 0 3vw;
		display: flex;
		align-items: center;
	}
	
	.wrapper .form-box li .title {
		flex: 0 0 18vw;
		font-size: 3vw;
		font-weight: 700;
		color: #666;
	}
	
	.wrapper .form-box li .content {
		flex: 1;
	}
	
	.wrapper .form-box li .content input {
		border: none;
		outline: none;
		width: 100%;
		height: 4vw;
		font-size: 3vw;
	}
	
	.wrapper .button-cz {
		width: 100%;
		box-sizing: border-box;
		padding: 4vw 3vw 0 3vw;
	}
	
	.wrapper .button-cz button {
		width: 100%;
		height: 10vw;
		font-size: 3.8vw;
		font-weight: 700;
		color: #fff;
		background-color: #38CA73;
		border-radius: 4px;
		border: none;
		outline: none;
	}
</style>
